#pragma once

#include <pthread.h>
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <unistd.h>
#include "sem.hpp"
using namespace std;

// #define DataType int
// #define TDataBlock RingQueue<DataType>
// #define TYPE Thread<RingQueue<DataType>>

// void DownLoad()
// {
//     cout << "......" << endl;
// }

class Thread
{
    using func_t = function<void(string)>;

public:
    void Excute()
    {
        _func(_threadname);
    }
public:
    Thread(func_t func, const std::string &name="none-name")
        : _func(func), _threadname(name), _stop(true)
    {}
    
    static void *threadroutine(void *args) // 类成员函数，形参是有this指针的！！
    {
        Thread *self = static_cast<Thread *>(args);
        self->Excute();
        return nullptr;
    }

    bool Start()
    {
        int n = pthread_create(&_tid, nullptr, threadroutine, this);
        if(!n)
        {
            _stop = false;
            return true;
        }
        else
        {
            return false;
        }
    }

    void Detach()
    {
        if(!_stop)
        {
            pthread_detach(_tid);
        }
    }

    void Join()
    {
        if(!_stop)
        {
            pthread_join(_tid, nullptr);
        }
    }

    std::string name()
    {
        return _threadname;
    }
    void Stop()
    {
        _stop = true;
    }
    ~Thread() {}

private:
    pthread_t _tid;
    std::string _threadname;
    func_t _func;
    bool _stop;
};